# @Author：silence
# @Email：792245022@qq.com
# @Date：2025/3/12 3:37

# https://www.gushiwen.cn/user/login.aspx?from=http://www.gushiwen.cn/user/collect.aspx
# https://www.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fwww.gushiwen.cn%2fuser%2fcollect.aspx

import requests
from lxml import etree

url = "https://www.gushiwen.cn/user/login.aspx?from=http://www.gushiwen.cn/user/collect.aspx"

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
}

response = requests.get(url=url, headers=headers)

content = response.text

tree = etree.HTML(content)

view_state = tree.xpath("//input[@id='__VIEWSTATE']/@value")[0]

view_state_generator = tree.xpath("//input[@id='__VIEWSTATEGENERATOR']/@value")[0]

code = tree.xpath("//img[@id='imgCode']/@src")[0]
code_url = f"https://www.gushiwen.cn{code}"

# session 会话
session = requests.session()
# 验证码的 url 内容
response_code = session.get(code_url)
content_code = response_code.content

# 保存验证码图片
with open("code.jpg", "wb") as f:
    f.write(content_code)

code_name = input("请输入验证码：")

url_post = "https://www.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fwww.gushiwen.cn%2fuser%2fcollect.aspx"

data_post = {
    "__VIEWSTATE": view_state,
    "__VIEWSTATEGENERATOR": view_state_generator,
    "from": "http://www.gushiwen.cn/user/collect.aspx",
    "email": "792245022@qq.com",
    "pwd": "mwl19971028",
    "code": code_name,
    "denglu": "登录"
}

response_post = session.post(url, headers=headers, data=data_post)

content_post = response_post.text

with open("gushiwen.html", "w", encoding="utf-8") as f:
    f.write(content_post)